import { RootState } from '@/types/store'
import { useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'

/**
 * useInitState
 * @param action 异步action
 * @param stateName store中的状态的名字 login, profile
 * @returns 状态
 */
export default function useInitState<Key extends keyof RootState> (action:()=>{}, stateName:Key) {
  const dispatch = useDispatch()
  useEffect(() => {
    dispatch(action())
  }, [])
  const res = useSelector((state:RootState) => state[stateName])
  return res
}
